//
//  OIPublicDetailViewController.m
//  EducationOID
//
//  Created by slz on 2017/6/2.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "OIPublicDetailViewController.h"
#import "OIPublicOrgnazitionModel.h"
#import "OIPublicOrgnationItem.h"

#import <objc/message.h>

@interface PublicDetailGroupItemView : UIView
@property (nonatomic,strong) UILabel *titleLabel;
@end

@interface PublicDetailGroupView : UIView
@property (nonatomic,strong) UILabel *titleLabel;
-(instancetype)initWithTitles:(NSArray *)titles;
@end



@interface OIPublicDetailViewController ()<MFWDataObserver>
@property (nonatomic,strong) PubListTableHeadView *headerView;
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UIView *contentView;
@end


@implementation PublicDetailGroupItemView

-(instancetype)init{
    self = [super init];
    if (self){
        _titleLabel = [UILabel new];
        _titleLabel.fontSize = 14;
        _titleLabel.textColor = kLight_TextColor_Default;
        [self addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.and.bottom.mas_equalTo(self);
            make.left.mas_equalTo(self.mas_left).offset(11);
        }];
    }
    return self;
}

@end

@implementation PublicDetailGroupView

-(instancetype)initWithTitles:(NSArray *)titles{
    self = [super init];
    if (self){
        _titleLabel = [UILabel new];
        _titleLabel.fontSize = 15;
        _titleLabel.textColor = kDark_TextColor_Default;
        [self addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self);
            make.height.mas_equalTo(30);
            make.top.mas_equalTo(self);
            make.left.mas_equalTo(self.mas_left).offset(11);
        }];
        for (int i = 0; i < titles.count; i ++){
            PublicDetailGroupItemView * itemView = [[PublicDetailGroupItemView alloc]init];
            itemView.tag = i + 1;
            [self addSubview:itemView];
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.and.right.mas_equalTo(self);
                make.top.mas_equalTo(i * 25 + 30);
                make.height.mas_equalTo(25);
            }];
            NSString * title = titles[i];
            itemView.titleLabel.text = [NSString stringWithFormat:@"%@: ",title];
        }
        UIView *lineView = [UIView new];
        lineView.backgroundColor = kLineColor_Gray_Default;
        [self addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(11);
            make.right.mas_equalTo(self.mas_right).offset(-11);
            make.bottom.mas_equalTo(self.mas_bottom);
            make.height.mas_equalTo(0.5);
        }];
    }
    return self;
}

- (void)updateTitles:(NSArray *)titles item:(OIPublicOrgnationItem *)item andIndex:(int)index{
    for (int i = 1; i <= titles.count; i ++){
        PublicDetailGroupItemView * itemView = [self viewWithTag:i];
        NSString * firstString = itemView.titleLabel.text;
        SEL seletor = NSSelectorFromString(titles[i - 1]);
        if ([item respondsToSelector:seletor]){
            NSString *string = ((NSString * (*)(id, SEL))objc_msgSend)(item, seletor);
            itemView.titleLabel.text = [NSString stringWithFormat:@"%@%@",firstString,string];
        }
    }
}

@end

@implementation OIPublicDetailViewController
{
    NSArray *_sourceTitle1;
    NSArray *_sourceTitle2;
    NSArray *_methodList1;
    NSArray *_methodList2;
    OIPublicOrgnazitionModel *_model;
    PublicDetailGroupView * _groupView1;
    PublicDetailGroupView * _groupView2;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestDetailInfoWithId:self.itemId];
    self.showNavigationBar = YES;
    [self setControllerTitle:@"信息中心"];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem * item = [UIBarButtonItem itemWithImage:@"top_back_1" selectImage:nil addTarget:self andAction:@selector(backAction)];
    self.navigationItem.leftBarButtonItem = item;
    
    
    _sourceTitle1 = @[@"单位或组织名称",
                      @"组织或单位英文名称",
                      @"组织或者单位地址",
                      @"组织或者单位英文地址",
                      @"组织/单位网址（URL）",
                      @"组织/单位规模（人数）",
                      @"机构性质",
                      @"机构电话",
                      @"法人代表",
                      @"邮政编码"];
    
    _sourceTitle2 = @[@"真实姓名",
                      @"性别",
                      @"职务",
                      @"联系地址",
                      @"邮箱"];
  
    
    _methodList1 = @[@"orz_name",
                     @"orz_name_en",
                     @"orz_address",
                     @"orz_address_en",
                     @"orz_url",
                     @"orz_people_number",
                     @"orz_nature",
                     @"orz_phone_number",
                     @"orz_ceo_name",
                     @"orz_short_number"];
    
    _methodList2 = @[@"vip_info_name",
                     @"vip_info_gender",
                     @"vip_info_position",
                     @"vip_info_address",
                     @"vip_info_email"];
    
    [self initHeaderView];

    // Do any additional setup after loading the view.
}

- (void)initHeaderView{
    _headerView = [[PubListTableHeadView alloc]init];
    _headerView.titleLabel.text = @"OID注册公示列表";
    _headerView.size = CGSizeMake(kScreenWidth, 80);
    
    
    _scrollView = [UIScrollView new];
    [self.view addSubview:_scrollView];
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    _contentView = [UIView new];
    [_scrollView addSubview:_contentView];
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
        make.height.mas_equalTo(140 + ((_sourceTitle1.count + _sourceTitle2.count) *25));
    }];
    
    [_contentView addSubview:_headerView];
    
    _groupView1 = [[PublicDetailGroupView alloc]initWithTitles:_sourceTitle1];
    _groupView1.titleLabel.text = @"机构详细信息";
    [_contentView addSubview:_groupView1];
    [_groupView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.mas_equalTo(_contentView);
        make.height.mas_equalTo(_sourceTitle1.count * 25 + 30);
        make.top.mas_equalTo(_headerView.mas_bottom);
    }];
    
    _groupView2 = [[PublicDetailGroupView alloc]initWithTitles:_sourceTitle2];
    _groupView2.titleLabel.text = @"机构详细信息";
    [_contentView addSubview:_groupView2];
    [_groupView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.mas_equalTo(_contentView);
        make.height.mas_equalTo(_sourceTitle1.count * 25 + 30);
        make.top.mas_equalTo(_groupView1.mas_bottom);
    }];
    
}

-(void)requestDetailInfoWithId:(NSString *)actionId {
    if(_model == nil) {
        _model = [OIPublicOrgnazitionModel managedModelInstance];
        [[_model class] addDataObserver:self];
    }
    [_model requestRefresh:[_model dataWithActionId:actionId]];
}

-(void)requestFinishedForTask:(MFWTask *)task withModel:(MFWDataModel *)model dataId:(NSString *)dataId requestType:(MFWDataRequestType)type itemArray:(NSArray *)itemArray success:(BOOL)bSuccess error:(NSError *)error{
   
    if (bSuccess && !error){
        MFWJson * json = [model jsonDataForDataId:dataId];
        OIPublicOrgnationItem * item = [[OIPublicOrgnationItem alloc]initWithJson:json];
        [_groupView1 updateTitles:_methodList1 item:item andIndex:0];
        [_groupView2 updateTitles:_methodList2 item:item andIndex:(int)_methodList1.count - 1];
    }
}

- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
